﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Accounts;
using Ext.Net;

namespace TestSuiteExtNET
{
    public partial class Demo2_1 : System.Web.UI.Page
    {
        StoreReadDataEventArgs e;

        protected void Page_Load(object sender, EventArgs e)
        {
            AccountsController ac = new AccountsController();

            Store m_store = this.dtvwAccounts.GetStore();
            m_store.DataSource = ac.GetUserStats();
            m_store.DataBind();
        }

        [DirectMethod]
        protected void UserGroupSelected(object sender, DirectEventArgs e)
        {
            string json = string.Empty;
            List<Accounts.BLL.AccountStats> obj = null;

            if ((e.ExtraParams != null) && (e.ExtraParams.Count() > 0))
            {
                json = e.ExtraParams["Values"];
                obj = JSON.Deserialize<List<Accounts.BLL.AccountStats>>(json);
            }
            else
            {
                Accounts.BLL.AccountStats acs = (Accounts.BLL.AccountStats)this.grdAccounts.Tag;
                obj = new List<Accounts.BLL.AccountStats>();
                obj.Add(acs);
            }
            //List<Accounts.BLL.AccountStats> obj = JSON.Deserialize<List<Accounts.BLL.AccountStats>>(json);

            //Dictionary<string, string>[] obj2 = JSON.Deserialize<Dictionary<string, string>[]>(json);

            X.Msg.Notify("Choose usernames starts with: {0}", obj[0].FirstChar).Show();

            AccountsController ac = new AccountsController();
            List<Accounts.BLL.Account> accountList = ac.GetAccounts(obj[0].FirstChar);

            this.grdAccounts.Tag = obj[0];

            Store grdStore = this.grdAccounts.GetStore();
            grdStore.DataSource = accountList;
            grdStore.DataBind();
        }

        protected void RefreshData(object sender, StoreReadDataEventArgs e)
        {
            //string tpl = "Start: {0}<br />Limit: {1}<br />Sort: {2}<br />Dir: {3}";

            //this.Label1.Text = e.Sort[0].Property + ":" + e.Sort[0].Direction.ToString();

            this.e = e;

            this.grdAccountsStore.DataBind();

            //UserGroupSelected(sender, new DirectEventArgs(null));
        }
    }
}